SlideShare a Scribd company logo
Class No.04  Data Structures http://ecomputernotes.com
C++ Code for Linked List // position current before the first // list element void start() { lastCurrentNode = headNode; currentNode = headNode; }; http://ecomputernotes.com
C++ Code for Linked List void remove() { if( currentNode != NULL &&  currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 6 7 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com
C++ Code for Linked List void remove() { if( currentNode != NULL &&  currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 6 7 1 headNode currentNode size=5 lastcurrentNode 8 1 1 http://ecomputernotes.com
C++ Code for Linked List void remove() { if( currentNode != NULL &&  currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 7 1 headNode currentNode size=5 lastcurrentNode 8 1 1 2 2 http://ecomputernotes.com
C++ Code for Linked List void remove() { if( currentNode != NULL &&  currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 7 1 headNode currentNode size=4 lastcurrentNode 8 1 1 2 2 3 3 4 4 http://ecomputernotes.com
C++ Code for Linked List int length()  {  return size;  }; private: int size; Node *headNode; Node *currentNode, *lastCurrentNode; http://ecomputernotes.com
Example of List Usage #include <iostream> #include <stdlib.h> #include &quot;List.cpp&quot; int main(int argc, char *argv[]) { List list; list.add(5); list.add(13); list.add(4); list.add(8); list.add(24); list.add(48); list.add(12); list.start(); while (list.next())  cout << &quot;List Element: &quot;<< list.get()<<endl; } http://ecomputernotes.com
Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. http://ecomputernotes.com
Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation http://ecomputernotes.com
Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list http://ecomputernotes.com
Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list back moving the current pointer back one node requires traversing the list from the start until the node whose next pointer points to current node.
Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. http://ecomputernotes.com
Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. http://ecomputernotes.com
Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. To avoid this we can use  two  pointers in a node: one to point to next node and another to point to the previous node: element next prev http://ecomputernotes.com
Doubly-Linked List Node class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node* getNext() { return nextNode; }; void setNext(Node* nextNode)  { this->nextNode = nextNode; }; Node* getPrev() { return prevNode; }; void setPrev(Node* prevNode)  { this->prevNode = prevNode; }; private: int object; Node* nextNode; Node* prevNode; };    http://ecomputernotes.com
Doubly-linked List  Need to be more careful when adding or removing a node. Consider add: the order in which pointers are reorganized is important: size=5 2 6 8 7 1 head current http://ecomputernotes.com
Doubly-linked List  newNode->setNext( current->getNext() ); size=5 2 6 8 7 head current 1 9 newNode 1 http://ecomputernotes.com
Doubly-linked List  newNode->setNext( current->getNext() ); newNode->setprev( current ); size=5 2 6 8 7 head current 1 9 newNode 1 2 http://ecomputernotes.com
Doubly-linked List  newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); size=5 2 6 8 7 head current 1 9 newNode 1 2 3 http://ecomputernotes.com
Doubly-linked List  newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); size=5 2 6 8 7 head current 1 9 newNode 1 2 3 4 http://ecomputernotes.com
Doubly-linked List  newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); current = newNode; size++; size=6 2 6 8 7 head current 1 9 newNode 1 2 3 4 http://ecomputernotes.com
Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL.  Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a  circularly-linked list . http://ecomputernotes.com
Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL.   Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a  circularly-linked list . http://ecomputernotes.com
Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL.  Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a  circularly-linked list . http://ecomputernotes.com
Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL.  Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a  circularly-linked list . http://ecomputernotes.com
Cicularly Linked List Two views of a circularly linked list: 2 6 8 7 1 head current size=5 2 8 7 1 head current size=5 6 http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. http://ecomputernotes.com
Josephus Problem A case where circularly linked list comes in handy is the solution of the  Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains.
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 

More Related Content

PDF
Auckland Programming Algorithms and Performance Meetup about Stacks & LeetCod...
PPTX
Linq introduction
PDF
Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg www.jdays.se
PDF
3. python intro
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 17
PPT
computer notes - Data Structures - 5
Auckland Programming Algorithms and Performance Meetup about Stacks & LeetCod...
Linq introduction
Facebook Graph Search by Ole martin mørk for jdays2013 Gothenburg www.jdays.se
3. python intro
computer notes - Data Structures - 25
computer notes - Data Structures - 22
computer notes - Data Structures - 17
computer notes - Data Structures - 5

Viewers also liked (20)

PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 11
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 28
computer notes - Data Structures - 7
computer notes - Data Structures - 21
computer notes - Data Structures - 6
computer notes - Data Structures - 1
computer notes - Data Structures - 20
computer notes - Data Structures - 12
computer notes - Data Structures - 14
computer notes - Data Structures - 8
computer notes - Data Structures - 11
computer notes - Deleting a node
computer notes - Data Structures - 16
computer notes - Data Structures - 23
computer notes - Data Structures - 38
computer notes - Data Structures - 35
computer notes - Data Structures - 19
computer notes - Data Structures - 9
computer notes - Data Structures - 29
computer notes - Data Structures - 18
computer notes - Data Structures - 39
computer notes - Data Structures - 28
Ad

Similar to computer notes - Data Structures - 4 (20)

PPTX
Deleting a node from the list(SINGLE LINKED LIST)
PPTX
Circular Linked List in Data Structures Design
PDF
computer notes - Circular list
PPTX
Data structure and algorithm list structures
DOC
Linked List
PPTX
Linked list data structures and algorithms
PPTX
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
PPT
linked list2.ppt linked list part 2 ppt
PDF
Linked List Problems
PPTX
Data structures
PPT
CH02- Data Structures and Algorithms.ppt
PDF
Circular linked list
PPT
computer notes - Data Structures - 2
PPTX
LINKED LISTS
PPTX
DSA(1).pptx
PPTX
Circular linked list
PPT
Chapter14
PPT
Linked lists
PDF
computer notes - Linked list
Deleting a node from the list(SINGLE LINKED LIST)
Circular Linked List in Data Structures Design
computer notes - Circular list
Data structure and algorithm list structures
Linked List
Linked list data structures and algorithms
DSL Unit 4 (Linked list) (PPT)SE3rd sem sppu.pptx
linked list2.ppt linked list part 2 ppt
Linked List Problems
Data structures
CH02- Data Structures and Algorithms.ppt
Circular linked list
computer notes - Data Structures - 2
LINKED LISTS
DSA(1).pptx
Circular linked list
Chapter14
Linked lists
computer notes - Linked list
Ad

More from ecomputernotes (17)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 31
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Attachment Theory What Childhood Says About Your Relationships.pdf
PPTX
diasspresentationndkcnskndncelklkfndc.pptx
PPTX
Learn how to prevent Workplace Incidents?
PPT
proper hygiene for teenagers for secondary students .ppt
PPTX
Self -Management and Self Awareness.pptx
PPTX
Identity Development in Adolescence.pptx
PDF
The Zeigarnik Effect by Meenakshi Khakat.pdf
PPTX
Learn how to use Portable Grinders Safely
PDF
Elle Lalli on The Role of Emotional Intelligence in Entrepreneurship
PPTX
Learn numerology content and join tarot reading
PPTX
Learn about numerology and do tarot reading
PDF
Top 10 Visionary Entrepreneurs to Watch in 2025
PPT
cypt-cht-healthy-relationships-part1-presentation-v1.1en.ppt
PPTX
Pradeep Kumar Roll no.30 Paper I.pptx....
PPTX
Attitudes presentation for psychology.pptx
PPTX
How to Deal with Imposter Syndrome for Personality Development?
PPTX
SELF ASSESSMENT -SNAPSHOT.pptx an index of yourself by Dr NIKITA SHARMA
PDF
The Power of Pausing Before You React by Meenakshi Khakat
PDF
Red Light Wali Muskurahat – A Heart-touching Hindi Story
PPTX
Personal Development - By Knowing Oneself?
Attachment Theory What Childhood Says About Your Relationships.pdf
diasspresentationndkcnskndncelklkfndc.pptx
Learn how to prevent Workplace Incidents?
proper hygiene for teenagers for secondary students .ppt
Self -Management and Self Awareness.pptx
Identity Development in Adolescence.pptx
The Zeigarnik Effect by Meenakshi Khakat.pdf
Learn how to use Portable Grinders Safely
Elle Lalli on The Role of Emotional Intelligence in Entrepreneurship
Learn numerology content and join tarot reading
Learn about numerology and do tarot reading
Top 10 Visionary Entrepreneurs to Watch in 2025
cypt-cht-healthy-relationships-part1-presentation-v1.1en.ppt
Pradeep Kumar Roll no.30 Paper I.pptx....
Attitudes presentation for psychology.pptx
How to Deal with Imposter Syndrome for Personality Development?
SELF ASSESSMENT -SNAPSHOT.pptx an index of yourself by Dr NIKITA SHARMA
The Power of Pausing Before You React by Meenakshi Khakat
Red Light Wali Muskurahat – A Heart-touching Hindi Story
Personal Development - By Knowing Oneself?

computer notes - Data Structures - 4

  • 1. Class No.04 Data Structures http://ecomputernotes.com
  • 2. C++ Code for Linked List // position current before the first // list element void start() { lastCurrentNode = headNode; currentNode = headNode; }; http://ecomputernotes.com
  • 3. C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 6 7 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com
  • 4. C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 6 7 1 headNode currentNode size=5 lastcurrentNode 8 1 1 http://ecomputernotes.com
  • 5. C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 7 1 headNode currentNode size=5 lastcurrentNode 8 1 1 2 2 http://ecomputernotes.com
  • 6. C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 2 7 1 headNode currentNode size=4 lastcurrentNode 8 1 1 2 2 3 3 4 4 http://ecomputernotes.com
  • 7. C++ Code for Linked List int length() { return size; }; private: int size; Node *headNode; Node *currentNode, *lastCurrentNode; http://ecomputernotes.com
  • 8. Example of List Usage #include <iostream> #include <stdlib.h> #include &quot;List.cpp&quot; int main(int argc, char *argv[]) { List list; list.add(5); list.add(13); list.add(4); list.add(8); list.add(24); list.add(48); list.add(12); list.start(); while (list.next()) cout << &quot;List Element: &quot;<< list.get()<<endl; } http://ecomputernotes.com
  • 9. Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. http://ecomputernotes.com
  • 10. Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation http://ecomputernotes.com
  • 11. Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list http://ecomputernotes.com
  • 12. Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list back moving the current pointer back one node requires traversing the list from the start until the node whose next pointer points to current node.
  • 13. Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. http://ecomputernotes.com
  • 14. Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. http://ecomputernotes.com
  • 15. Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node: element next prev http://ecomputernotes.com
  • 16. Doubly-Linked List Node class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node* getNext() { return nextNode; }; void setNext(Node* nextNode) { this->nextNode = nextNode; }; Node* getPrev() { return prevNode; }; void setPrev(Node* prevNode) { this->prevNode = prevNode; }; private: int object; Node* nextNode; Node* prevNode; };   http://ecomputernotes.com
  • 17. Doubly-linked List Need to be more careful when adding or removing a node. Consider add: the order in which pointers are reorganized is important: size=5 2 6 8 7 1 head current http://ecomputernotes.com
  • 18. Doubly-linked List newNode->setNext( current->getNext() ); size=5 2 6 8 7 head current 1 9 newNode 1 http://ecomputernotes.com
  • 19. Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); size=5 2 6 8 7 head current 1 9 newNode 1 2 http://ecomputernotes.com
  • 20. Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); size=5 2 6 8 7 head current 1 9 newNode 1 2 3 http://ecomputernotes.com
  • 21. Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); size=5 2 6 8 7 head current 1 9 newNode 1 2 3 4 http://ecomputernotes.com
  • 22. Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); current = newNode; size++; size=6 2 6 8 7 head current 1 9 newNode 1 2 3 4 http://ecomputernotes.com
  • 23. Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . http://ecomputernotes.com
  • 24. Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . http://ecomputernotes.com
  • 25. Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . http://ecomputernotes.com
  • 26. Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list . http://ecomputernotes.com
  • 27. Cicularly Linked List Two views of a circularly linked list: 2 6 8 7 1 head current size=5 2 8 7 1 head current size=5 6 http://ecomputernotes.com
  • 28. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . http://ecomputernotes.com
  • 29. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. http://ecomputernotes.com
  • 30. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. http://ecomputernotes.com
  • 31. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. http://ecomputernotes.com
  • 32. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. http://ecomputernotes.com
  • 33. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. http://ecomputernotes.com
  • 34. Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem . Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains.
  • 35. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 http://ecomputernotes.com
  • 36. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 37. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 38. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 39. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 40. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 41. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 42. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 43. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 44. Josephus Problem N=10, M=3 9 8 7 6 5 4 3 2 1 10 eliminated http://ecomputernotes.com
  • 45. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 46. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 47. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 48. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 49. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 50. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 51. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 
  • 52. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 
  • 53. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; } 

Editor's Notes

  • #3: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #4: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #5: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #6: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #7: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #8: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #9: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #10: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #11: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #12: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #13: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #14: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #15: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #16: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #17: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #18: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #19: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #20: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #21: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #22: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #23: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #24: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #25: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #26: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #27: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #28: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #29: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #30: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #31: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #32: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #33: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #34: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #35: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #36: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #37: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #38: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #39: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #40: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #41: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #42: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #43: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #44: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #45: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #46: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #47: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #48: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #49: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #50: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #51: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #52: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #53: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #54: End of lecture 4. The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.